<?php

namespace App\libraries\Logging;

use Monolog\Formatter\JsonFormatter as BaseJsonFormatter;

class JsonFormatter extends BaseJsonFormatter
{
    /**
     * @param array $record
     *
     * @return string
     */
    public function format(array $record): string
    {
        $newRecord = [
            'request_id' => defined('REQUEST_ID') ? REQUEST_ID : '',
            'app_id' => config('app.name') ?? '',
            'env' => config('app.env') ?? '',
            'instance_id' => gethostname() ?? '',
            'time' => $record['datetime']->format('Y-m-d H:i:s.u'),
            'level' => $record['level_name'],
            'message' => $record['message'],
            'context' => $record['context'] ?? [],
        ];
        return $this->toJson($this->normalize($newRecord), true) . ($this->appendNewline ? "\n" : '');
    }
}
